// 蜂窝蜜造平台生成代码，此文件只生成一次，不需要添加到 .beeignore

package com.fowo.api.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.fowo.api.entity.SupplierFlowNode;
import com.fowo.api.entity.SupplierMyTask;
import com.fowo.api.entity.SupplierTempTask;
import com.fowo.api.model.supplier.flow.node.SupplierFlowNodeEnum;
import com.fowo.api.model.supplier.my.task.SupplierMyTaskVo;
import com.fowo.api.service.SupplierMyTaskService;
import com.fowo.api.sys.annotation.DataActivity;
import com.fowo.api.sys.entity.enums.SysDataActivityAction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 供应商自建任务 服务实现类
 */
@Service
@Slf4j
public class SupplierMyTaskServiceImpl extends SupplierMyTaskBaseServiceImpl implements SupplierMyTaskService {
    @Resource
    private SupplierFlowNodeServiceImpl supplierFlowNodeService;

    @DataActivity(action = SysDataActivityAction.Add)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long create(SupplierMyTaskVo model) throws Exception {
        Assert.isTrue(ObjUtil.isNotEmpty(model), "参数异常!");
        try {
            SupplierFlowNode node = supplierFlowNodeService.getNode(model.getEnCode(),null);
            Long id = super.create(model);
            model.setId(id);
            supplierFlowNodeService.process(CollUtil.newArrayList(model), node, node.getNode(), null, item -> {
            });
            return id;
        } catch (Exception ex) {
            log.error("发生异常,异常信息:" + StrUtil.subPre(ExceptionUtil.getRootCauseMessage(ex), 2000));
            throw new RuntimeException("发生异常,信息:" + StrUtil.subPre(ExceptionUtil.getRootCauseMessage(ex), 2000));
        }

    }

    @DataActivity(
            action = SysDataActivityAction.Other,
            message = "%s执行了提交操作"
    )
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void doSubmit(List<Long> ids) throws Exception {
        Assert.isTrue(CollUtil.isNotEmpty(ids), "参数异常!");
        List<SupplierMyTask> list = this.listByIds(ids);
        SupplierFlowNode node = supplierFlowNodeService.getConditionNode(list);
        supplierFlowNodeService.process(list, node, node.getNode(), CollUtil.newArrayList(node.getNodeUp()), item -> {
        });
        this.updateBatchById(list);
    }

    /**
     * 取消提交
     */
    @DataActivity(
            action = SysDataActivityAction.Other,
            message = "%s执行了取消提交操作"
    )
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void unDoSubmit(List<Long> ids) throws Exception {
        Assert.isTrue(CollUtil.isNotEmpty(ids), "参数异常!");
        List<SupplierMyTask> list = this.listByIds(ids);
        SupplierFlowNode node = supplierFlowNodeService.getConditionNode(list);
        supplierFlowNodeService.process(list, node, node.getNodeExceptionStatus(), CollUtil.newArrayList(node.getNodeUp()), item -> {
            Assert.isTrue(StrUtil.isNotBlank(node.getNodeExceptionStatus()), "没有配置异常节点!");
        });
        this.updateBatchById(list);
    }

    /**
     * 撤销
     */
    @DataActivity(
            action = SysDataActivityAction.Other,
            message = "%s执行了撤销操作"
    )
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void doRevoke(List<Long> ids) throws Exception {
        Assert.isTrue(CollUtil.isNotEmpty(ids), "参数异常!");
        List<SupplierMyTask> list = this.listByIds(ids);
        SupplierFlowNode node = supplierFlowNodeService.getConditionNode(list);
        supplierFlowNodeService.process(list, node, SupplierFlowNodeEnum.关闭.getValue(), CollUtil.newArrayList( SupplierFlowNodeEnum.已提交.getValue(), SupplierFlowNodeEnum.已接收.getValue()), item -> {
            Assert.isTrue(StrUtil.isNotBlank(node.getNodeExceptionStatus()), "没有配置异常节点!");
        });
        this.updateBatchById(list);
    }

    /**
     * 完成
     */
    @DataActivity(
            action = SysDataActivityAction.Other,
            message = "%s执行了完成操作"
    )
    @Override
    public void doFinish(List<Long> ids) throws Exception {
        Assert.isTrue(CollUtil.isNotEmpty(ids), "参数异常!");
        List<SupplierMyTask> list = this.listByIds(ids);
        SupplierFlowNode node = supplierFlowNodeService.getConditionNode(list);
        supplierFlowNodeService.process(list, node, node.getNode(), CollUtil.newArrayList(node.getNodeUp()), item -> {
        });
        this.updateBatchById(list);
    }
}
